<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      canvas {
        border: 1px solid #ccc;
        margin-right: 5px;
      }
    </style>
  </head>
  <body>
    <script>
       (()=>{
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext("2d");

        ctx.strokeStyle = '#f00';
        ctx.setLineDash([10]) ;
        ctx.strokeRect(100,100,200,200);
      

        ctx.translate(100,100);

        ctx.rect(100,100,200,200);
        ctx.fill();

       })();

       (()=>{
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        //基于原坐标系绘制的图形
        const ctx = canvas.getContext("2d");

        ctx.fillRect(0,0,100,100) ;

        ctx.translate(100,100);


        //绘制坐标系
        ctx.beginPath();
        ctx.moveTo(-400,0);
        ctx.lineTo(400,0);
        ctx.moveTo(0,-400);
        ctx.lineTo(0,400);
        ctx.stroke();

        ctx.beginPath();
        ctx.arc(0,0,6,0,Math.PI*2) ;
        ctx.fill();

        for(let i=-400;i<=400;i+=10){
          ctx.beginPath();
          ctx.moveTo(i,-5);
          ctx.lineTo(i,5);
          ctx.stroke();

          ctx.beginPath();
          ctx.moveTo(-5,i);
          ctx.lineTo(5,i);
          ctx.stroke();
        }

        //坐标系中绘制图形
        ctx.fillRect(100,100,100,100) ;


       })();
    </script>

  </body>
</html>
